package com.onesports.intelligent.k12.polarlight.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConditionalOnClass(WxPayService.class)
public class WxPayConfiguration {
  @Resource
  private ApplicationProperties properties;

  @Bean
  @ConditionalOnMissingBean
  public WxPayService wxService() {
    WxPayConfig payConfig = new WxPayConfig();
    payConfig.setAppId(StringUtils.trimToNull(this.properties.getWxPay().getAppId()));
    payConfig.setMchId(StringUtils.trimToNull(this.properties.getWxPay().getMchId()));
    payConfig.setMchKey(StringUtils.trimToNull(this.properties.getWxPay().getMchKey()));
    payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getWxPay().getSubMchId()));
    payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getWxPay().getKeyPath()));
    payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.properties.getWxPay().getPrivateKeyPath()));
    payConfig.setPrivateCertPath(StringUtils.trimToNull(this.properties.getWxPay().getPrivateCertPath()));
    payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getWxPay().getApiV3Key()));
    // 可以指定是否使用沙箱环境
    payConfig.setUseSandboxEnv(false);
    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(payConfig);
    return wxPayService;
  }

}
